package com.example.coinhelper.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.coinhelper.Adapter.NoteAdapter;
import com.example.coinhelper.Bean.Details;
import com.example.coinhelper.Bean.Note;
import com.example.coinhelper.Manage.NoteManager;
import com.example.coinhelper.R;
import com.example.coinhelper.utils.MySesseion;

import java.util.List;

public class NoteActivity extends AppCompatActivity {
    private ImageView addImageView;
    private List<Note> noteList;
    private NoteManager noteManager;
    private NoteAdapter noteAdapter;
    private ListView noteListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        addImageView = findViewById(R.id.note_addnote);
        noteManager = new NoteManager(this);
        noteListView = findViewById(R.id.note_listview);
        Inteview();
        addImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent(NoteActivity.this, AddNoteActivity.class);
                 startActivity(intent);
            }
        });
        noteListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Note clickedNote = noteList.get(position);
                Intent intent = new Intent(NoteActivity.this, AddNoteActivity.class);
                System.out.println(clickedNote.toString());
                intent.putExtra("title", clickedNote.getTitle());
                intent.putExtra("id",  String.valueOf(clickedNote.getId()));
                intent.putExtra("body", clickedNote.getBody());
                startActivity(intent);
            }
        });
        noteListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取被长按的笔记对象
                Note clickedNote = noteList.get(position);
                AlertDialog.Builder builder = new AlertDialog.Builder(NoteActivity.this);
                builder.setTitle("确认删除？")
                        .setMessage("确定要删除这条笔记吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // 确认删除操作
                                noteManager.deleteById(clickedNote);
                                noteList = noteManager.getAllNotesByUsername(MySesseion.USER.getUsername());
                                noteAdapter.setNoteList(noteList);
                                noteAdapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // 用户取消了删除操作
                                dialog.dismiss(); // 关闭对话框
                            }
                        })
                        .show();
                return true;
            }
        });


    }
    @Override
    public void onResume() {
        super.onResume();
        Inteview();
    }
    private void Inteview(){
        noteList = noteManager.getAllNotesByUsername(MySesseion.USER.getUsername());
        for(Note note:noteList){
            System.out.println(note.toString());
        }
        noteAdapter = new NoteAdapter(this,noteList);
        noteListView.setAdapter(noteAdapter);
    }
}